package com.huawei.appmarket.oobe.app;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager;
import android.os.Build.VERSION;
import android.text.TextUtils;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat.d;
import com.huawei.appmarket.R;
import com.huawei.appmarket.oobe.activity.OOBEAppGalleryActivity;
import com.huawei.appmarket.oobe.activity.OOBEAppPermissionActivity;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.secure.android.common.intent.SafeBroadcastReceiver;
import com.huawei.secure.android.common.intent.SafeIntent;
import java.util.LinkedHashMap;
import o.cei;
import o.dld;
import o.dli;
import o.dll;
import o.dlm;
import o.dln;
import o.dlo;
import o.Logger;
import o.dof;
import o.doo;
import o.ContentManager;
import o.eqp;
import o.ll;

@RequiresApi(api = 24)
public class OOBEFlowController extends SafeBroadcastReceiver {
    private static OOBEFlowController ˏ = new OOBEFlowController();

    public static void init(Context context) {
        int componentEnabledSetting = context.getPackageManager().getComponentEnabledSetting(new ComponentName(context, OOBEAppGalleryActivity.class));
        if (componentEnabledSetting == 1 || componentEnabledSetting == 0) {
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction("com.huawei.appmarket.oobe.ACTION_NOTIFICATION_FLOW_FINISHED");
            intentFilter.addAction("com.huawei.appmarket.oobe.app.action.STORE_DOWNLOAD_TASK_FINISHED");
            intentFilter.addAction("com.huawei.appmarket.oobe.ACTION_SUPPORT_QUERY_FINISHED");
            intentFilter.addAction("com.huawei.appmarket.oobe.ACTION_SHOW_NOTIFICATION");
            intentFilter.addAction("com.huawei.appmarket.oobe.ACTION_QUEREY_IF_OOBE_SUPPORT");
            intentFilter.addAction("com.huawei.appmarket.oobe.app.action.ACTION_OOBE_ALL_DONE");
            intentFilter.addAction("com.huawei.appmarket.oobe.ACTION_GET_APPLIST_FINISHED");
            ll.ˏ(context).ˏ(ˏ, intentFilter);
            Logger.ˎ("OOBEFlowController", "OOBEFlowController registered ");
        }
    }

    public static void unInit(Context context) {
        try {
            ll.ˏ(context).ˋ(ˏ);
        } catch (IllegalArgumentException e) {
            Logger.ˎ("OOBEFlowController", "uninit exception");
        }
    }

    public void onReceiveMsg(Context context, Intent intent) {
        if (intent != null) {
            String action = intent.getAction();
            Logger.ˎ("OOBEFlowController", "OOBEFlowController action=" + action);
            if (action != null) {
                boolean z = true;
                switch (action.hashCode()) {
                    case -1762509884:
                        if (action.equals("com.huawei.appmarket.oobe.ACTION_QUEREY_IF_OOBE_SUPPORT")) {
                            z = true;
                            break;
                        }
                        break;
                    case -1678471586:
                        if (action.equals("com.huawei.appmarket.oobe.app.action.ACTION_OOBE_ALL_DONE")) {
                            z = true;
                            break;
                        }
                        break;
                    case -597822204:
                        if (action.equals("com.huawei.appmarket.oobe.ACTION_SHOW_NOTIFICATION")) {
                            z = true;
                            break;
                        }
                        break;
                    case 175990230:
                        if (action.equals("com.huawei.appmarket.oobe.ACTION_CHECK_SERVICE_ZONE")) {
                            z = true;
                            break;
                        }
                        break;
                    case 296089858:
                        if (action.equals("com.huawei.appmarket.oobe.ACTION_SUPPORT_QUERY_FINISHED")) {
                            z = true;
                            break;
                        }
                        break;
                    case 869581572:
                        if (action.equals("com.huawei.appmarket.oobe.ACTION_GET_APPLIST_FINISHED")) {
                            z = true;
                            break;
                        }
                        break;
                    case 1841849150:
                        if (action.equals("com.huawei.appmarket.oobe.app.action.STORE_DOWNLOAD_TASK_FINISHED")) {
                            z = true;
                            break;
                        }
                        break;
                    case 2001020888:
                        if (action.equals("com.huawei.appmarket.oobe.ACTION_NOTIFICATION_FLOW_FINISHED")) {
                            z = false;
                            break;
                        }
                        break;
                }
                switch (z) {
                    case false:
                        dlm.ˋ();
                        dlm.ˊ();
                        return;
                    case true:
                        OOBEDownloadJobService.schedule(context);
                        return;
                    case true:
                        ˊ(context, (SafeIntent) intent);
                        return;
                    case true:
                        ॱ(context);
                        return;
                    case true:
                        unInit(context);
                        ˊ(context);
                        return;
                    case true:
                        int intExtra = intent.getIntExtra("fromWhere", 0);
                        if (eqp.ʼ() || dli.ॱॱ(context)) {
                            Intent intent2 = new Intent("com.huawei.appmarket.oobe.ACTION_SUPPORT_QUERY_FINISHED");
                            intent2.addFlags(268435456);
                            intent2.putExtra("isSupport", false);
                            intent2.putExtra("maxDisplay", 0);
                            intent2.putExtra("fromWhere", intExtra);
                            ll.ˏ(context).ˋ(intent2);
                            return;
                        }
                        dlo.ˎ().ˏ();
                        dlm.ˎ(intExtra);
                        return;
                    case true:
                        ˏ(context);
                        return;
                    case true:
                        ˎ(context, (SafeIntent) intent);
                        return;
                    default:
                        Logger.e("OOBEFlowController", "default process");
                        return;
                }
            }
        }
    }

    private void ˊ(Context context, SafeIntent safeIntent) {
        dli.ˊ();
        boolean booleanExtra = safeIntent.getBooleanExtra("isSupport", false);
        int intExtra = safeIntent.getIntExtra("maxDisplay", 18);
        boolean booleanExtra2 = safeIntent.getBooleanExtra("simChangeNotify", false);
        boolean booleanExtra3 = safeIntent.getBooleanExtra("notificationNotRemovable", false);
        boolean booleanExtra4 = safeIntent.getBooleanExtra("notificationFlowSupport", true);
        boolean booleanExtra5 = safeIntent.getBooleanExtra("appUpdateNotify", false);
        int intExtra2 = safeIntent.getIntExtra("appListCheckIntervalTime", 14);
        String str = null;
        String str2 = null;
        int i = 0;
        if (dli.ˋ()) {
            str = safeIntent.getStringExtra("channelInfo");
            str2 = safeIntent.getStringExtra("isOpenChannel");
            i = safeIntent.getIntExtra("policy", 0);
        }
        Editor edit = dli.ˊ(context).edit();
        edit.putInt("maxDisplay", intExtra).putInt("appListCheckIntervalTime", intExtra2).putBoolean("simChangeNotify", booleanExtra2).putBoolean("notificationNotRemovable", booleanExtra3).putBoolean("notificationFlowSupport", booleanExtra4).putBoolean("appUpdateNotify", booleanExtra5);
        if (dli.ˋ()) {
            if (booleanExtra) {
                edit.putInt("policy", i);
            }
            if (booleanExtra) {
                edit.putString("isOpenChannel", str2).putString("channelInfo", str);
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append("save cota, isOpenChannel=").append(str2).append(", channelInfo=").append(str).append(", policy =").append(i);
                Logger.ˎ("OOBEFlowController", stringBuffer.toString());
                ˋ(str, str2, i);
            }
        }
        edit.commit();
        if (dli.ˏ(context)) {
            ˏ(context, booleanExtra, safeIntent.getStringExtra("layoutData"));
        } else {
            ˏ(booleanExtra, booleanExtra4, booleanExtra2, booleanExtra5, safeIntent.getIntExtra("fromWhere", 0));
        }
        LinkedHashMap ʽ = dli.ʽ(context);
        ʽ.put("isSupport", String.valueOf(booleanExtra ? 1 : 0));
        dli.ˏ("350206", ʽ);
    }

    private void ˏ(boolean z, boolean z2, boolean z3, boolean z4, int i) {
        Context ˋ = ContentManager.getInstant().getContext();
        if (dli.ˋ(ˋ) == 2) {
            dli.ˎ(ˋ);
        } else if (!z) {
            Logger.e("OOBEFlowController", "doQueryResultForNotificationFLow : not support, skipped");
            dlo.ˎ().ॱ();
        } else if (dli.ˋ()) {
            int ʼॱ = dld.ˎ().ʼॱ();
            int ʾ = dld.ˎ().ʾ();
            if (ʾ > 0 && ʾ != ʼॱ) {
                Logger.e("OOBEFlowController", "oobePolicy not match, skipped");
                dlo.ˎ().ॱ();
            } else if (ʼॱ == 1) {
                ˋ(z, z2, z3, z4, i);
                ˋ(ˋ);
            } else if (ʼॱ == 2) {
                if (i == 1) {
                    if (dli.ˎ(ˋ, "com.ironsource.appcloud.oobe.huawei")) {
                        Logger.ˎ("OOBEFlowController", "send cotaReady broadcast to IS");
                        dln o_dln = new dln();
                        o_dln.ˎ(dli.ʼ(ˋ));
                        o_dln.ˏ(dli.ˊ(ˋ).getString("isOpenChannel", "1"));
                        o_dln.ˏ().clear();
                        o_dln.ˏ().add(dld.ˎ().ʿ());
                        dll.ˎ().ˏ(ˋ, o_dln);
                    } else {
                        ˋ();
                        dld.ˎ().ॱ(false);
                    }
                }
                dlo.ˎ().ॱ();
                OOBESimStateReceiver.disable();
            } else {
                Logger.e("OOBEFlowController", "invalid oobePolicy");
            }
        } else {
            ˋ(z, z2, z3, z4, i);
        }
    }

    private void ˋ(boolean z, boolean z2, boolean z3, boolean z4, int i) {
        if (!z) {
            return;
        }
        if ((z2 && i == 1) || ((z3 && i == 2) || (z4 && i == 3))) {
            dlm.ˋ(i);
        } else {
            Logger.ˎ("OOBEFlowController", "no notification as switch is off, fromWhere=" + i);
        }
    }

    private void ˋ(String str, String str2, int i) {
        Object obj = 1;
        if (TextUtils.isEmpty(str) || !(("1".equals(str2) || "0".equals(str2)) && (1 == i || 2 == i))) {
            obj = null;
        }
        if (obj == null) {
            Logger.ˋ("OOBE", "checkCotaInfo failed");
        }
    }

    private void ˏ(Context context, boolean z, String str) {
        if (z) {
            if (!dli.ˋ()) {
                dld.ˎ().ˏ(str);
                dlm.ˋ(0);
            } else if (dld.ˎ().ʼॱ() == 1) {
                dld.ˎ().ˏ(str);
                dlm.ˋ(0);
            } else if (dld.ˎ().ʼॱ() == 2) {
                if (dli.ˎ(context, "com.ironsource.appcloud.oobe.huawei")) {
                    dld.ˎ().ॱ(z);
                    if (doo.ˊ(context)) {
                        dll.ˎ().ˋ(context);
                    }
                } else {
                    dld.ˎ().ॱ(false);
                    ˋ();
                    z = false;
                }
                ˋ(context, z);
                dld.ˎ();
                dld.ˋ(true);
            }
        } else if (dli.ˋ()) {
            dld.ˎ().ॱ(z);
            ˋ(context, z);
        } else {
            ॱ(context, z);
        }
    }

    private void ˎ(Context context, SafeIntent safeIntent) {
        boolean booleanExtra = safeIntent.getBooleanExtra("resultOk", false);
        int intExtra = safeIntent.getIntExtra("fromWhere", 0);
        if (!dli.ˏ(context)) {
            if (dli.ˋ()) {
                dld.ˎ().ॱ(booleanExtra);
            }
            if (booleanExtra && intExtra != 0) {
                OOBEShowNotificationJobService.schedule(context);
            }
            dlo.ˎ().ॱ();
        } else if (dli.ˋ()) {
            dld.ˎ().ॱ(booleanExtra);
            ˋ(context, booleanExtra);
        } else {
            ॱ(context, booleanExtra);
        }
    }

    private void ˊ(Context context) {
        if (context != null) {
            dli.ͺ(context);
            if (VERSION.SDK_INT > 25) {
                JobScheduler jobScheduler = (JobScheduler) context.getSystemService("jobscheduler");
                if (jobScheduler.getPendingJob(1002) != null) {
                    jobScheduler.cancel(1002);
                }
                if (jobScheduler.getPendingJob(1003) != null) {
                    jobScheduler.cancel(1003);
                }
                if (jobScheduler.getPendingJob(1005) != null) {
                    jobScheduler.cancel(1005);
                }
                PackageManager packageManager = context.getPackageManager();
                packageManager.setComponentEnabledSetting(new ComponentName(context, OOBEAppGalleryActivity.class), 2, 1);
                packageManager.setComponentEnabledSetting(new ComponentName(context, OOBEAppPermissionActivity.class), 2, 1);
                packageManager.setComponentEnabledSetting(new ComponentName(context, OOBEQueryIfSupportJobService.class), 2, 1);
                packageManager.setComponentEnabledSetting(new ComponentName(context, OOBEShowNotificationJobService.class), 2, 1);
                packageManager.setComponentEnabledSetting(new ComponentName(context, OOBEExportedReceiver.class), 2, 1);
                packageManager.setComponentEnabledSetting(new ComponentName(context, OOBELocaleChangeReceiver.class), 2, 1);
                packageManager.setComponentEnabledSetting(new ComponentName(context, OOBEAppListUpdateCheckJobService.class), 2, 1);
                packageManager.setComponentEnabledSetting(new ComponentName(context, OOBESimStateReceiver.class), 2, 1);
                packageManager.setComponentEnabledSetting(new ComponentName(context, OOBELauncherService.class), 2, 1);
            }
            Logger.ˎ("OOBEFlowController", "doOOBEFinalization done");
        }
    }

    private void ॱ(Context context) {
        boolean z = dli.ˊ(context).getBoolean("notificationNotRemovable", false);
        d ˎ = new d(context).ˊ(R.drawable.appmarket_notify_smallicon).ˏ(context.getString(R.string.oobe_flow_start_notification_title)).ˊ(context.getString(R.string.oobe_flow_start_notification_content)).ˎ(-1);
        ˎ.ˎ(PendingIntent.getActivity(context, 0, new Intent(context, OOBEAppGalleryActivity.class), HwAccountConstants.FLAG_TRANS_NAVIGATION_BAR));
        Intent intent = new Intent("com.huawei.appmarket.oobe.ACTION_NOTIFICATION_REMOVED");
        intent.setClass(context, OOBEExportedReceiver.class);
        ˎ.ˊ(PendingIntent.getBroadcast(context, 0, intent, HwAccountConstants.FLAG_TRANS_NAVIGATION_BAR));
        if (z) {
            ˎ.ˏ(false);
        }
        Notification ˊ = ˎ.ˊ();
        if (z) {
            ˊ.flags |= 32;
        }
        cei.ˏ(context, "OOBE", 2018012701, ˊ);
        dli.ˏ("350205", dli.ʽ(context));
        Logger.ˎ("OOBEFlowController", "OOBE notification showed");
    }

    private void ˏ(Context context) {
        String string = dli.ˊ(context).getString("serviceZone", "");
        if (!dof.ˋ(string) && !dld.ˎ().ॱˋ().equals(string)) {
            dli.ͺ(context);
        }
    }

    private void ॱ(Context context, boolean z) {
        Intent intent = new Intent("com.huawei.appmarket.oobe.ACTION_OOBE_SUPPORT_RESULT");
        intent.putExtra("isSupport", z);
        context.sendBroadcast(intent, "com.huawei.appmarket.oobe.permission.SEND_SETUP_WIZARD_EVENTS");
        Logger.ˎ("OOBEFlowController", "OOBEFlowController.doSupportQueryResult, send result in primary flow, isSupport=" + z);
        dld.ˎ().ˊ(false);
    }

    private void ˋ(Context context) {
        if (((JobScheduler) context.getSystemService("jobscheduler")).getPendingJob(1005) == null) {
            OOBEAppListUpdateCheckJobService.schedule(context);
        }
    }

    private void ˋ(Context context, boolean z) {
    }

    private void ˋ() {
        dli.ˏ("350306", dli.ʽ(ContentManager.getInstant().getContext()));
    }
}
